#include "stdafx.h"
#include "ylSessionData.h"
#include "ylLogMessageClass.h"

using namespace yoctolog_viewer;


SessionData::SessionData()
{
	RemoteAddress = "";

	m_mapMessageTemplates = gcnew System::Collections::Generic::Dictionary<unsigned int, LogMessageTemplate ^>();
	m_mapMessages = gcnew System::Collections::Generic::Dictionary<unsigned int, LogMessage ^>();
	m_mapGroups = gcnew System::Collections::Generic::Dictionary<System::String ^, LogMessageGroup ^>();
	m_mapMessageClasses = gcnew System::Collections::Generic::Dictionary<unsigned char, LogMessageClass ^>();
	m_mapCallstackEntries = gcnew System::Collections::Generic::Dictionary<unsigned int, CallstackEntry ^>();

	TotalBytesRead = 0;
	TotalBytesProcessed = 0;
	TotalMsgsProcessed = 0;

	m_uiCurrentID = 0;
}


SessionData::~SessionData()
{
	delete m_mapGroups;
	delete m_mapMessages;
	delete m_mapMessageTemplates;
	delete m_mapMessageClasses;
	delete m_mapCallstackEntries;
}


void SessionData::AddMessageTemplate(unsigned int uiID, LogMessageTemplate ^pTemplate)
{
	m_mapMessageTemplates[uiID] = pTemplate;
}


void SessionData::AddMessage(LogMessage ^pMessage)
{
	m_mapMessages[pMessage->m_uiID] = pMessage;
}


void SessionData::GetObjectData(System::Runtime::Serialization::SerializationInfo ^info, System::Runtime::Serialization::StreamingContext context)
{
	info->AddValue("m_mapMessageTemplates", m_mapMessageTemplates);
	info->AddValue("m_mapMessages", m_mapMessages);
	info->AddValue("m_mapGroups", m_mapGroups);
	info->AddValue("m_mapMessageClasses", m_mapMessageClasses);
	info->AddValue("m_mapCallstackEntries", m_mapCallstackEntries);
	info->AddValue("RemoteAddress", RemoteAddress);
	info->AddValue("ComputerName", ComputerName);
	info->AddValue("StartedDateTime", StartedDateTime);
	info->AddValue("TotalBytesRead", TotalBytesRead);
	info->AddValue("TotalBytesProcessed", TotalBytesProcessed);
	info->AddValue("TotalMsgsProcessed", TotalMsgsProcessed);

}


SessionData::SessionData(System::Runtime::Serialization::SerializationInfo ^info, System::Runtime::Serialization::StreamingContext context)
{
	m_mapMessageTemplates = (System::Collections::Generic::Dictionary<unsigned int, LogMessageTemplate ^> ^)info->GetValue("m_mapMessageTemplates", System::Collections::Generic::Dictionary<unsigned int, LogMessageTemplate ^>::typeid);
	m_mapMessages = (System::Collections::Generic::Dictionary<unsigned int, LogMessage ^> ^)info->GetValue("m_mapMessages", System::Collections::Generic::Dictionary<unsigned int, LogMessage ^>::typeid);
	m_mapGroups = (System::Collections::Generic::Dictionary<System::String ^, LogMessageGroup ^> ^)info->GetValue("m_mapGroups", System::Collections::Generic::Dictionary<System::String ^, LogMessageGroup ^>::typeid);
	m_mapMessageClasses = (System::Collections::Generic::Dictionary<unsigned char, LogMessageClass ^> ^)info->GetValue("m_mapMessageClasses", System::Collections::Generic::Dictionary<unsigned char, LogMessageClass ^>::typeid);
	m_mapCallstackEntries = (System::Collections::Generic::Dictionary<unsigned int, CallstackEntry ^> ^)info->GetValue("m_mapCallstackEntries", System::Collections::Generic::Dictionary<unsigned int, CallstackEntry ^>::typeid);
	RemoteAddress = info->GetString("RemoteAddress");
	ComputerName = info->GetString("ComputerName");
	StartedDateTime = (System::DateTime^)info->GetValue("StartedDateTime", System::DateTime::typeid);
	TotalBytesRead = info->GetInt32("TotalBytesRead");
	TotalBytesProcessed = info->GetInt32("TotalBytesProcessed");
	TotalMsgsProcessed = info->GetInt32("TotalMsgsProcessed");
}


LogMessage ^SessionData::GetMessage( unsigned int uiID )
{
	if( m_mapMessages->ContainsKey(uiID) )
		return m_mapMessages[uiID];
	
	return nullptr;
}
